(function($) {
	$.extend({
		main: {}
	});
	$.extend($.main, {
		init: function() {
			this.clickEvents();
			this.swiper();
			this.liandong();
			this.add();
			this.qu();
			this.street();
			this.clickScroll();
		},
		clickEvents: function() {
			$(".g-select-control").click(function() {
				$(this).next().css("display", "block");
			});
			$(".g-select-down").mouseleave(function() {
				$(".g-select-down").css("display", "none");
			});
			$(".g-select-down .option").click(function() {
				$(this).parent().prev().text($(this).text());
			});
			$(".g-select-down .option").hover(function() {
				$(this).children().css("display", "block");
			});
		},
		swiper: function() {
			var mySwiper = new Swiper('.swiper-container', {
				direction: 'horizontal', // 垂直切换选项
				loop: true, // 循环模式选项

				// 如果需要分页器
				pagination: {
					el: '.swiper-pagination',
				},

				// 如果需要前进后退按钮
				navigation: {
					nextEl: '.swiper-button-next',
					prevEl: '.swiper-button-prev',
				},

				autoplay: {
					delay: 3000
				}
			})
			mySwiper.el.onmouseover = function() {
				mySwiper.navigation.$nextEl.removeClass('hide');
				mySwiper.navigation.$prevEl.removeClass('hide');
			}
			mySwiper.el.onmouseout = function() {
				mySwiper.navigation.$nextEl.addClass('hide');
				mySwiper.navigation.$prevEl.addClass('hide');
			}
			mySwiper.params.pagination.clickable = true;
			//此外还需要重新初始化pagination
			mySwiper.pagination.destroy()
			mySwiper.pagination.init()
			mySwiper.pagination.bullets.eq(0).addClass('swiper-pagination-bullet-active');
		},
		//三级联动
		liandong: function() {
			$.get("data/city.json", function(data) {
				data.forEach(function(item) {
					$(".izk-city-nav").append(`<li data-id="${item.id}">${item.city}</li>`);
				});
				$(".izk-city-nav").eq(0).addClass("cur")
				$(".izk-city-nav").append(`<p class="service-li-btn"><a>点击查找更多</a></p>`);
			});
		},
		qu: function() {
			var city = $(".izk-city-nav li.cur").data("id");
			if(city) {
				$.get("data/country.json", function(data) {
					$(".list_nav").empty()
					data.forEach(function(item) {
						if(city.toString() === item.cityid) {
							$(".list_nav").append(`<li data-id="${item.id}">${item.country}</li>`);
						};
					});
					$(".list_nav li").eq(0).addClass("active")
					$.main.street()
				});
			}
		},
		street: function() {
			var country = $(".list_nav li.active").data("id");
			if(country) {
				$.get("data/street.json", function(data) {
					$(".list_bd").empty()
					data.forEach(function(item) {

						if(country.toString() === item.countryid) {
							$(".list_bd").append(`<li>${item.stree}</li>`)
						}
					})
				})
			}
		},
		//地址轮播
		add: function() {
			var i = 0;
			var index = 1;
			var timer;
			var timer1
			star()

			function star() {
				timer = setInterval(function() {
					var a = $(".izk-city-nav li");
					a.eq(i).addClass("cur").siblings().removeClass("cur");
					country()
					i++;
					if(i >= a.length) i = 0;
					$.main.qu();
					$.main.street();
				}, 1000);
			}

			$(".izk-city-nav").on("mouseover", "li", function() {
				clearInterval(timer);
				clearInterval(timer1)
				var thisid = $(this).index();
				$(this).addClass("cur").siblings().removeClass("cur");
				$.main.qu();
				$.main.street();
				i = thisid + 1;

			});
			$(".izk-city-nav").on("mouseout", "li", function() {
				country()
			});

			function country() {
				index = 1
				clearInterval(timer);
				timer1 = setInterval(function() {
					var a = $(".list_nav li");
					a.eq(index).addClass("active").siblings().removeClass("active");
					index++;
					$.main.street();
					if(index >= a.length) {
						clearInterval(timer1);
						star();
					}
				}, 1000);

			}
			$(".list_nav").on("mouseover", "li", function() {
				clearInterval(timer1)
				var thisid = $(this).index();
				$(this).addClass("active").siblings().removeClass("active");
				index = thisid;
				$.main.street();

			});
			$(".list_nav").on("mouseout", "li", function() {
				country()
			});
		},
		clickScroll: function() {
			var i = 0;
			var divs = 0;
			$(".left").click(function() {
				divs += 250
				if(divs > 0) {
					divs = 0
					$(".white").css("margin-left", 0)
				} else {
					$(".white").css({
						"transform":`translateX(${divs}px)`,
						"transition":"0.5s"
					})
				}

			});
			$(".right").click(function() {
				divs -= 250;
				if(divs <= -1750) {
					divs = 0
					$(".white").css("margin-left", divs)
				}
				$(".white").css({
						"transform":`translateX(${divs}px)`,
						"transition":"0.5s"
					})
			});

	

		}

	});
})(jQuery);